package com.zzq.my.jetpacks.jectpackapp.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SizeUtils;
import com.zzq.my.jetpacks.jectpackapp.R;


/*
  * ================================================
  * 描述：自定义view实现半圆弧进度条
  * 作者：zhuangzeqin
  * 时间: 2023年6月5日08:55:00
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  */
public class CircleProgressView extends View {
    private Paint outPaint;

    private Paint innerPaint;

    private Paint mTextPaint;//达标状态
    private Paint mProgressTextPaint;//进度百分百

    private RectF oval;

    //最大进度 默认100
    private int max = 100;
    //当前进度默认0
    private int progress = 0;
    //底部文本固定达标进度内容
    private String text = "达标进度";
    private String txtProgress = "0%";
    private int mProgressColor;
    //圆弧宽度
    private int roundWidth = SizeUtils.dp2px(10);

    private int mCircleRadius = SizeUtils.dp2px(0);//圆点大小
    private Paint mCirclePaint;
    private float[] pos =new float[2];
//    private final int[] colors = {Color.parseColor("#95ACFF"),Color.parseColor("#4379FF")};

    private int viewWidth; //宽度--控件所占区域

    private float nowPro = 0;//用于动画

    private ValueAnimator animator;//动画

    public CircleProgressView(Context context) {
        super(context);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs, context);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs, context);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initAttrs(attrs, context);
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        txtProgress = this.progress+"%";
        mCircleRadius = SizeUtils.dp2px(this.progress<=0? 8 :0);//进度为0的话显示圆点大小
        //进度条动画
        this.setAnimator();
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;

    }

    /**
     * 初始化数据
     * @param attr
     * @param context
     */
    private void initAttrs(AttributeSet attr, Context context) {

        outPaint = new Paint();
//        outPaint.setColor(Color.parseColor("#F5F6FA"));
        //未达到进度的部分
        outPaint.setColor(ContextCompat.getColor(this.getContext(), R.color.color_EBF3FD));
        outPaint.setAntiAlias(true);
        outPaint.setStyle(Paint.Style.STROKE);
        outPaint.setStrokeCap(Paint.Cap.ROUND);
        outPaint.setStrokeWidth(SizeUtils.dp2px(8));

        innerPaint = new Paint();
//        innerPaint.setColor(Color.parseColor("#667CFF"));
        innerPaint.setColor(ContextCompat.getColor(this.getContext(), R.color.color_EBF3FD));
        innerPaint.setAntiAlias(true);
        innerPaint.setStyle(Paint.Style.STROKE);
        innerPaint.setStrokeCap(Paint.Cap.ROUND);
        innerPaint.setStrokeWidth(SizeUtils.dp2px(12));

        //todo 1文本部分
        mProgressTextPaint = new Paint();
        mProgressTextPaint.setAntiAlias(true);
//        mProgressTextPaint.setColor(Color.parseColor("#333333"));
        mProgressTextPaint.setColor(ContextCompat.getColor(this.getContext(), R.color.gray_color_333333));
        mProgressTextPaint.setFakeBoldText(true);
        mProgressTextPaint.setTextSize(SizeUtils.sp2px(28));

        //todo 2文本部分
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
//        mTextPaint.setColor(Color.parseColor("#666666"));
        mTextPaint.setColor(ContextCompat.getColor(this.getContext(), R.color.color_666666));
        mTextPaint.setFakeBoldText(false);
        mTextPaint.setTextSize(SizeUtils.sp2px(14));
        //todo 进度条默认的颜色
        mProgressColor = ContextCompat.getColor(this.getContext(), R.color.color_3887EB);

        //进度条动画
        this.setAnimator();
    }

    /**
     * 未达标的颜色设置
     * @param progressColor
     * @param unProgressColor
     */
    public void setNotUpToStandardColor(int progressColor ,int unProgressColor)
    {
        txtProgress = "未达标";
        mProgressTextPaint.setColor(ContextCompat.getColor(this.getContext(), R.color.gray_color_333333));
        mProgressColor = ContextCompat.getColor(this.getContext(), progressColor);
        outPaint.setColor(ContextCompat.getColor(this.getContext(), unProgressColor));
        innerPaint.setColor(ContextCompat.getColor(this.getContext(), unProgressColor));
        setAnimator();
    }
    /**
     * 达标的颜色设置
     * @param progressColor
     * @param unProgressColor
     */
    public void setUpToStandardColor(int progressColor ,int unProgressColor)
    {
        txtProgress = "已达标";
//        mCircleRadius = SizeUtils.dp2px(0);//圆点大小
        mProgressColor = ContextCompat.getColor(this.getContext(), progressColor);
        mProgressTextPaint.setColor(mProgressColor);
        outPaint.setColor(ContextCompat.getColor(this.getContext(), unProgressColor));
        innerPaint.setColor(ContextCompat.getColor(this.getContext(), unProgressColor));
        setAnimator();
    }
    /**
     * 进度条动画
     */
    private void setAnimator()
    {
        animator = ValueAnimator.ofFloat(0, progress);
        animator.setDuration(100);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                nowPro = (float) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        animator.start();
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        final int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, (widthSpecSize / 2) + (int) (Math.cos(20) * (widthSpecSize / 2)));

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        viewWidth = w;//得到宽度以此来计算控件所占实际大小
        //计算画布所占区域
        oval = new RectF();
        oval.left = roundWidth + getPaddingLeft();
        oval.top = roundWidth + getPaddingTop();
        oval.right = viewWidth - roundWidth - getPaddingRight();
        oval.bottom = viewWidth - roundWidth - getPaddingBottom();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Path outerPath = new Path();
        //只用来画弧形  forceMoveTo 参数的意思是，
        //     绘制是要「抬一下笔移动过去」，
        //     还是「直接拖着笔过去」，区别在于是否留下移动的痕迹。
        outerPath.arcTo(oval,180,180,true);
        canvas.drawPath(outerPath,outPaint);
        //1 TODO 实现渐变进度条
//        SweepGradient sweepGradient = new SweepGradient(getWidth()/2,getWidth()/2,Color.parseColor("#FFFFFF"),Color.parseColor("#0047F7"));
        SweepGradient sweepGradient = new SweepGradient(getWidth()/2,getWidth()/2,mProgressColor,mProgressColor);
        innerPaint.setShader(sweepGradient);
        /**
         * oval：圆弧所在的椭圆对象。
         * startAngle：圆弧的起始角度。
         * sweepAngle：圆弧的角度。
         * useCenter：是否显示半径连线，true表示显示圆弧与圆心的半径连线，false表示不显示。
         * paint：绘制时所使用的画笔。
         */
        canvas.drawArc(oval, 180, 180 * nowPro / max, false, innerPaint); //绘制圆弧

        //圆点绘制
        mCirclePaint = new Paint();
//        mCirclePaint.setColor(Color.WHITE);
        mCirclePaint.setColor(mProgressColor);
        mCirclePaint.setStyle(Paint.Style.FILL);

        PathMeasure pathMeasure = new PathMeasure(outerPath,false);
        boolean posTan = pathMeasure.getPosTan(pathMeasure.getLength() * nowPro / max, pos, null);
        canvas.drawCircle(pos[0],pos[1],mCircleRadius,mCirclePaint);

        float textWidth = mTextPaint.measureText(text);
        float txtProgressWidth = mProgressTextPaint.measureText(txtProgress);
//        txtProgress = progress+"%";//百分百文本绘制 Y坐标-20dp
        if (TextUtils.equals(txtProgress, "未达标") ||TextUtils.equals(txtProgress, "已达标") || TextUtils.equals(txtProgress, "未开始"))//达标
            canvas.drawText(txtProgress, viewWidth / 2 - txtProgressWidth / 2, viewWidth / 2-SizeUtils.dp2px(20), mProgressTextPaint);
        else
        canvas.drawText((int)nowPro+"%", viewWidth / 2 - txtProgressWidth / 2, viewWidth / 2-SizeUtils.dp2px(20), mProgressTextPaint);
        canvas.drawText(text, viewWidth / 2 - textWidth / 2, viewWidth / 2, mTextPaint);
    }


}
